/**
 * @tag controllers, home
 */
jQuery.Controller.extend('Bugstat.Controllers.ReportListController',
/* @Static */
{
    onDocument: false
},
/* @Prototype */
{
	report_descriptors : [],
	successLoadCallback : 0,
	errorLoadCallback : 0,
	
	/**
	 * Load avilable report descriptors
	 */ 
    loadDescriptors: function( success, error ) 
	{
		this.successLoadCallback = success;
		this.errorLoadCallback = error;
		
		Bugstat.Models.ReportDescriptor.findAll( {}, this.callback('handleDescriptorsLoaded'), this.callback('handleErrorLoading'));
	},
	
	/**
	 * Display loaded discriptors in a view. New controller will
	 * be created for each descriptor (in case not already in place)
	 *
	 * @param {Function} success  A callback function that is called if loading succeded.
     * @param {Function} error   A callback function for an error in the ajax request.     	
	 */	
	handleDescriptorsLoaded: function( report_descriptors )
	{
		if ( report_descriptors )
		{
			this.report_descriptors = report_descriptors;

			// Make list visible and put empty descriptor tags in it
			$('#report_list').html('//bugstat/views/report_list/init.ejs', {report_descriptors:report_descriptors} );

			if ( this.successLoadCallback ) this.successLoadCallback();

			this.displayReports( report_descriptors );			
		}
		else
		{
			this.handleErrorLoading( 0, 0, 'cant_load_report_descriptors' );
		}
	},
	
	handleErrorLoading: function(request, error, status) 
	{
		if ( this.errorLoadCallback ) this.errorLoadCallback( request, error, status );
	},
	
	updateFilter: function( filter ) 
	{
		filter = filter.toLowerCase()
		var hasReports = false;

		if ( filter == '' )
		{
			this.displayReports( this.report_descriptors );
		}
		else
		{
			var result = [];

			for(var i = 0; i < this.report_descriptors.length ; i++)
			{
				var report_descriptor = this.report_descriptors[i];
				var name              = report_descriptor.name.toLowerCase();
				if ( name.indexOf(filter) != -1 )
				{
					result.push( report_descriptor );
				}
			}
			
			this.displayReports( result );
		}		
	},
	
	displayReports: function( report_descriptors ) 
	{
		$(".report-list-content").html( '//bugstat/views/report_list/list.ejs', this.createListStructure( report_descriptors ) );

		// Attach controller to each descriptor element
		for(var i = 0; i < report_descriptors.length ; i++)
		{
			var report_descriptor = $('.' + report_descriptors[i].identity() );
			report_descriptor.bugstat_report_descriptor();
			report_descriptor.bugstat_report_descriptor("initController", report_descriptors[i] );
		}		
		
		// Round corners
		$('.report-list').corner('5px');		
	},
	
	createListStructure: function( report_descriptors ) {
		var reports = [];
		var groups = [];
		var position = 0;
		for(var i = 0; i < report_descriptors.length ; i++) { 
			var group = report_descriptors[i].group;

		 	if ( group )
			{
				if ( !groups[group])
				{
					groups[group] = [];
					reports[position] = group;
					position += 1;
				}
				
				groups[group].push(report_descriptors[i]);
			}
			else	
			{
				reports[position] = report_descriptors[i];
				position += 1;
			}
		}
				
		return { groups: groups, reports:reports };
	},
	
	'.filterReports keyup': function(el)
	{	
    	this.updateFilter( el.val() );
    },    

	'.filterReports change': function(el)
	{	
		this.updateFilter( el.val() );
    }
});